package com.alon.spring01.utils;

import com.alon.spring01.common.config.spring.RestClient;
import com.alon.spring01.model.User;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;

/**
 * @program: spring01
 * @description: httpClient工具类
 * @author: yulongguang
 * @create: 2019-06-02 16:49
 **/
public class HttpClientUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtils.class);

    /**
     * post请求 带参数
     */
    public static String doPost(String url, Map<String, String> formParams) {
        if (MapUtils.isEmpty(formParams)) {
            return doPost(url);
        }

        try {
            MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
            formParams.keySet().forEach(key -> requestEntity.add(key, MapUtils.getString(formParams, key, "")));
            return RestClient.getClient().postForObject(url, requestEntity, String.class);
        } catch (Exception e) {
            LOGGER.error("POST请求出错：{}", url, e);
        }

        return Strings.EMPTY;
    }

    /**
     * post请求
     */
    public static String doPost(String url) {
        try {
            return RestClient.getClient().postForObject(url, HttpEntity.EMPTY, String.class);
        } catch (Exception e) {
            LOGGER.error("POST请求出错：{}", url, e);
        }

        return Strings.EMPTY;
    }

    /**
     * get 请求 带参数
     */
    public static String doGet(String url, Map<String, String> formParams) {
        if (MapUtils.isEmpty(formParams)) {
            doGet(url);
        }
        try {

            return RestClient.getClient().getForObject(url, String.class, formParams);
        } catch (RestClientException e) {
            LOGGER.error("GET请求出错：{}", url, e);
        }
        return Strings.EMPTY;
    }

    /**
     * get请求
     */
    public static String doGet(String url) {
        try {
            return RestClient.getClient().getForObject(url, String.class);
        } catch (Exception e) {
            LOGGER.error("GET请求出错：{}", url, e);
        }

        return Strings.EMPTY;
    }

    /**
     * delete
     */
    public static void doDelete(String url, Map<String, String> formParams) {
        if (MapUtils.isEmpty(formParams)) {
            doDelete(url);
        }
        try {
            RestClient.getClient().delete(url, formParams);
        } catch (RestClientException e) {
            LOGGER.error("DELETE 请求出错：{}", url, e);
        }
    }

    /**
     * delete
     */
    public static void doDelete(String url) {
        try {
            RestClient.getClient().delete(url);
        } catch (RestClientException e) {
            LOGGER.error("DELETE请求出错：{}", url, e);
        }
    }

    /**
     * exchange
     */
    public static String exchange(String url, Map<String, String> formParams) {
        //请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = MediaType.APPLICATION_JSON_UTF8;
        headers.setContentType(mediaType);
        HttpEntity<Resource> httpEntity = new HttpEntity<Resource>(headers);
        if (MapUtils.isEmpty(formParams)) {
            exchange(url, httpEntity);
        }
        try {
            ResponseEntity<String> exchange = RestClient.getClient().exchange(url, HttpMethod.GET, httpEntity, String.class, formParams);
            return exchange.getBody();
        } catch (RestClientException e) {
            LOGGER.error("exchange GET请求出错：{}", url, e);
        }
        return Strings.EMPTY;
    }

    /**
     * exchange
     */
    public static String exchange(String url, HttpEntity<Resource> httpEntity) {
        try {
            ResponseEntity<String> exchange = RestClient.getClient().exchange(url, HttpMethod.GET, httpEntity, String.class);
            return exchange.getBody();
        } catch (RestClientException e) {
            LOGGER.error("exchange GET请求出错：{}", url, e);
        }
        return Strings.EMPTY;
    }

    /**
     * exchange
     *
     * @param url
     * @return
     */
    public static String exchange(String url) {

        try {
            ResponseEntity<String> exchange = RestClient.getClient().exchange(url, HttpMethod.GET, null, String.class);
            return exchange.getBody();
        } catch (RestClientException e) {
            LOGGER.error("exchange GET请求出错：{}", url, e);
        }
        return Strings.EMPTY;
    }

    /**
     * @param url
     * @return
     */
    public static String exchangeAndPost(String url) {
        try {
            ResponseEntity<String> exchange = RestClient.getClient().exchange(url, HttpMethod.POST, null, String.class);
            return exchange.getBody();
        } catch (RestClientException e) {
            LOGGER.error("exchange POST请求出错：{}", url, e);
        }
        return Strings.EMPTY;
    }
}
